/*
 * @Copyright: Copyright (c) 2030 吴周海
 * @Author: wzh200x@126.com
 * @Date: 2025-10-10 11:35:00
 * @LastEditors: wzh200x@126.com
 * @LastEditTime: 2025-10-15 17:49:31
 * @FilePath: \gogamewebserver\goGameWebServer\internal\model\entity\cdKeyGroupModel.go
 * @Description: CdKeyGroup 实体模型
 */
package entity

import (
	"goGameWebServer/internal/common/logger"
	"goGameWebServer/internal/common/utils"
	"goGameWebServer/internal/defines/cerror"
	"goGameWebServer/internal/defines/dataType"
	"goGameWebServer/internal/interfaces"
	"goGameWebServer/internal/manager/databasemanager"

	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/util/gmeta"
)

type CdKeyGroupModelData struct {
	gmeta.Meta `orm:"table: cd_key_group"`
	Id         dataType.TypeDataBaseId `orm:"id"`         // ID
	Name       string                  `orm:"name"`       // 名称
	Extra      string                  `orm:"extra"`      // CDKey信息,返回到前端
	Desc       string                  `orm:"desc"`       // 描述
	Status     int                     `orm:"status"`     // 状态
	CreateTime string                  `orm:"createTime"` // 创建时间
}

func (d *CdKeyGroupModelData) Reset() {
	d.Id = 0
	d.Name = ""
	d.Extra = ""
	d.Desc = ""
	d.Status = 0
	d.CreateTime = ""
}

type CdKeyGroupModel struct {
	EntityModel
}

func (m *CdKeyGroupModel) LoadEntityDataFromDB(records interface{}) error {
	base, err := databasemanager.ManagerDatabase().DefaultDataBase()
	if err != nil {
		return err
	}
	if entityData, ok := m.EntityData.(*CdKeyGroupModelData); ok {
		one, err := base.GetDb().Model(m.Template).Where("id", entityData.Id).One()
		if err != nil {
			return err
		}
		if one == nil {
			return m.Create()
		}
		_ = one.Struct(m.EntityData)
		m.SetEntityDBID(entityData.Id)
		return nil
	}
	return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil), "cd_key_group model data is invalid")
}

func (m *CdKeyGroupModel) Create() error {
	if entityData, ok := m.EntityData.(*CdKeyGroupModelData); ok {
		entityData.CreateTime = utils.GetNowTimeFormat()
		err := m.CreateDataToDB()
		entityData.Id = m.EntityDBID
		if err != nil {
			return err
		}
		logger.LoggerIns().Debug("create cd_key_group success")
		return nil
	}
	return gerror.NewCodef(gcode.New(cerror.ERROR_ENTITY_PROPERTY_INVALID, "", nil), "cd_key_group model create fail, data is invalid")
}

func NewCdKeyGroupModel(name string) interfaces.IEntityModel {
	m := &CdKeyGroupModel{
		EntityModel: NewEntityModel(name),
	}
	m.SetEntityData(&CdKeyGroupModelData{
		Id:         0,
		Name:       "",
		Extra:      "",
		Desc:       "",
		Status:     0,
		CreateTime: "",
	})
	m.SetTemplate(m.EntityData)
	return m
}

func GetAllCdKeyGroup() ([]*CdKeyGroupModelData, error) {
	base, err := databasemanager.ManagerDatabase().DefaultDataBase()
	if err != nil {
		return nil, err
	}
	var list []*CdKeyGroupModelData
	re, err := base.GetDb().Model(&CdKeyGroupModelData{}).All()
	if err != nil {
		return nil, err
	}
	if re != nil {
		_ = re.Structs(&list)
	}
	return list, nil
}

func CdKeyGroupIdIsExist(id int) bool {
	base, err := databasemanager.ManagerDatabase().DefaultDataBase()
	if err != nil {
		return false
	}
	re, e := base.GetDb().Model(&CdKeyGroupModelData{}).Where("id", id).One()
	if e != nil {
		return false
	}
	return re != nil
}
